<?php

require_once realpath (dirname (__FILE__) . '//..//pages.php');
require_once CLASS_USER;

function arrayPrint ($array) {
    if (is_array ($array)) {
        foreach ($array as $value) {
            echo "$value,";
        }
        return true;
    } else {
        return false;
    }
}

function arrayRemove ($array, $keys) {
    $new = array ();
    if (is_array ($array)) {
        if (is_array ($keys)) {
            
        } else if (is_string ($keys)) {
            foreach ($array as $key => $value) {
                if ($key != $keys) {
                    $new[$key] = value;
                }
            }
        }
    } else {
        return false;
    }
}

/**
 * This method checks if current session's user is registered or authorizes him to do something
 * 
 * Calls checkSession to verify if session is active, then it verifies if user in $_SESSION at KEY_SESSION_USER is registered
 * 
 * @return boolean true if current session's user is registered 
 */
function authorize ($action = null, $what = null, $ID = null) {
    require_once CLASS_USER;
    if (isset ($_SESSION[KEY_SESSION_USER])) {
        $user = unserialize ($_SESSION[KEY_SESSION_USER]);
        if (is_a ($user, 'User')) {
            if (isset ($action) && !empty ($action)) {
                return $user->can ($action, $what, $ID);
            } else {
                return $user->isRegistered ();
            }
        }
    }
    return false;
}

function check ($where) {
    checkSession();
    if (isset ($_SESSION['ID']) && isset ($_SESSION['stat'])) {
        $ID = $_SESSION['ID'];
        $stat = $_SESSION['stat'];
        unset ($_SESSION['ID']);
        unset ($_SESSION['stat']);
        if (is_numeric ($ID)) {
            $where .= " AND $stat.ID=$ID";
        }
    }
    return $where;
}

/**
 * This method is used to check easily if particular indexes of $array are set
 * 
 * @param array $array the array that will be checked
 * @param string|array $indexes index that will be checked|array containing indexes that will be checked
 * @return boolean true if and only if all the indexes are set, else false
 */
function checkIndexe ($array, $indexes) {
    if (is_array ($array)) {
        if (is_array ($indexes)) {
            foreach ($indexes as $index) {
                if (!isset ($array[$index])) {
                    return false;
                }
            }
            return true;
        } else if (is_string ($indexes) || is_numeric ($indexes)) {
            return isset ($array[$indexes]);
        }
    } else {
        return false;
    }
}

/**
 * This method starts session if it isn't active.
 * 
 * Checks if $_SESSION is set. If true it doesn't do anything, else it calls session_start method
 * @return boolean true if session was already started, else false  
 */
function checkSession () {
    if (!isset ($_SESSION)) {
        return session_start ();
    }
    return true;
}

/**
 * This method is used to get easily a pre-defined doctype
 * @return string an echo-ready doctype
 */
function doctype () {
    return '<!DOCTYPE html>
';
}

/**
 * This method is used to build rapidly an head for the page
 * 
 * Supports only javascript!
 * 
 * @param string $title title that will be shown in browser's tab
 * @param string $stylesheet path of the stylesheet
 * @param string|array $scripts path of the script|array containg paths of the scrpits
 * @param string $html other html code that will be included before closing head
 * @return string an echo-ready head 
 */
function head ($title = null, $stylesheet = null, $scripts = null, $html = "") {
    $head = '
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>' . $title . '</title>';
    if (isset ($stylesheet)) {
        $less_stylesheet = false;
        if (is_string ($stylesheet)) {
            if (substr ("$stylesheet", strlen ($stylesheet) - 5, 5) == ".less") {
                $less_stylesheet = true;
                $sheet_type = 'stylesheet/less';
            } else {
                $sheet_type = "stylesheet";
            }
            $head .= '
    <link rel="' . $sheet_type . '" type="text/css" href="' . $stylesheet . '" />';
        } else if (is_array ($stylesheet)) {
            foreach ($stylesheet as $style) {
                if (substr ("$style", strlen ($style) - 5, 5) == ".less") {
                    $less_stylesheet = true;
                    $sheet_type = 'stylesheet/less';
                } else {
                    $sheet_type = "stylesheet";
                }
                $head .= '
    <link rel="' . $sheet_type . '" type="text/css" href="' . $style . '" />';
            }
        }
    }
    if (isset ($scripts)) {
        if (is_array ($scripts)) {
            foreach ($scripts as $script) {
                $head .= '
    <script type="text/javascript" src="' . $script . '"></script>';
            }
        } else {
            $head .= '
    <script type="text/javascript" src="' . $script . '"></script>';
        }
    }
    if ($less_stylesheet) {
        $head .= '
    <script src="' . HTTP_SCRIPT_LESS . '" type="text/javascript"></script>';
    }
    if (isset ($html)) {
        $head .= "
    $html";
    }
    $head .= '
</head>';
    return $head;
}

/**
 * This methosd is used for debugging purposes
 * 
 * Prints all the rows contained into $result showing indexes near the corresponding values
 * 
 * @param string $result resource from mysql_query
 */
function mysqlPrint ($result) {
    while ($row = mysql_fetch_assoc ($result)) {
        print_r ($row);
        echo '<br>';
    }
}

function notify ($msg) {
    require_once VW_NOTIFY;
    die ();
}

/**
 * Creates a new connection to the database
 * @return \Database the object used to handle the database
 */
function openConnection ($debug_mode = false) {
    $db = new Database (SERVER, USER, PASS, DATABASE);
    $db->setDebugMode ($debug_mode);
    $db->query ('SET CHARACTER SET utf8');
    return $db;
}

/**
 * This method is used to redirect to other pages
 * 
 * @param string $page the new page
 * @return string an echo-ready javascript
 */
function redirect ($page, $include_html_tags = false) {
    if (isset ($include_html_tags) && is_bool ($include_html_tags) && $include_html_tags) {
        $str = '
<html>
    <head></head>
    <body>';
    } else {
        $str = '';
    }
    $str .= '
        <script type="text/javascript">
            window.location.href = ' . $page . '
        </script>
';
    if (isset ($include_html_tags) && is_bool ($include_html_tags) && $include_html_tags) {
        $str .= '
    </body>
</html>';
    }
    return $str;
}

function sqlToXml ($host, $user, $pass, $database, $tablename, $query) {

    $link = mysql_connect ($host, $user, $pass) or die ("Could not connect: " . mysql_error ());
    $db = mysql_select_db ($database, $link) or die (mysql_error ());

    $result = mysql_query ($query);
    if (!$result) {
        die ('Invalid query: ' . mysql_error ());
    }

    $numOfCols = mysql_num_fields ($result);
    $numOfRows = mysql_num_rows ($result);

    $info = mysql_fetch_assoc ($result);

    //send headers 
    header ('Content-type: text/xml');
    header ('Pragma: public');
    header ('Cache-control: private');
    header ('Expires: -1');
    $xml = '<?xml version="1.0" encoding="utf-8"?>';
    $xml.= "<{$tablename}>";

    if ($numOfRows > 0) {
        do {
            $xml.= "<row>";
            foreach ($info as $column => $value) {
                $xml.= "<{$column}>{$value}</{$column}>";
            }
            $xml.= "</row>";
        } while ($info = mysql_fetch_array ($result));
    }
    $xml.= "</{$tablename}>";

    mysql_free_result ($result);
    return $xml;
}

?>
